<template>
  <Form ref="form" :model="form" :rules="rule" :label-width="100">
    <FormItem label="菜单名称" prop="name">
      <Input v-model="form.name" placeholder=""></Input>
    </FormItem>
    <FormItem label="路由名称" prop="access">
      <Input v-model="form.access" placeholder=""></Input>
    </FormItem>
    <FormItem label="图标" prop="icon">
      <Input v-model="form.icon" placeholder=""></Input>
    </FormItem>
    <FormItem label="排序" prop="displayOrder">
      <Input v-model="form.displayOrder" placeholder=""></Input>
    </FormItem>
    <FormItem label="菜单类型" prop="type">
      <Select v-model="form.type">
          <Option :value="1">菜单</Option>
          <Option :value="2">按钮</Option>
          <Option :value="3">接口</Option>
      </Select>
    </FormItem>
    <FormItem label="是否显示" prop="isShow">
      <RadioGroup v-model="form.isShow">
          <Radio :label="1">是</Radio>
          <Radio :label="0">否</Radio>
      </RadioGroup>
    </FormItem>
    <FormItem label="是否公开" prop="isPublic">
      <RadioGroup v-model="form.isPublic">
          <Radio :label="1">是</Radio>
          <Radio :label="0">否</Radio>
      </RadioGroup>
    </FormItem>
  </Form>
  </template>
<script>
import { save, update, get } from '@/api/sys/sys.resource.service.js'

export default {
  props: {
    isEdit: {
      type: Boolean,
      default: false
    },
    params: {
      type: Object,
      default () {
        return {}
      }
    }
  },
  data () {
    return {
      form: {
        id: 0,
        name: '',
        access: '',
        icon: '',
        displayOrder: '',
        type: 2,
        isShow: 0,
        isPublic: 0
      },
      rule: {
        name: [
          { required: true, message: '菜单名称不能为空', trigger: 'blur' }
        ],
        access: [
          { required: true, message: '路由名称不能为空', trigger: 'blur' }
        ],
        icon: [
          { required: false, message: '图标不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  mounted () {
    if (this.isEdit) {
      get({
        id: this.params.id
      }).then(res => {
        this.form = this.$util.copy(res.data, this.form)
      })
    }
  },
  methods: {
    submit () {
      return new Promise((resolve, reject) => {
        this.$refs['form'].validate((valid) => {
          if (valid) {
            if (this.isEdit) {
              update(this.form).then(res => {
                resolve(res)
              }).catch(e => {
                reject(e)
              })
            } else {
              delete this.form.id
              save(this.form).then(res => {
                resolve(res)
              }).catch(e => {
                reject(e)
              })
            }
          } else {
            reject(new Error('error'))
          }
        })
      })
    }
  }
}
</script>
